<?php
/**
 * Created by PhpStorm.
 * User: cc
 * Date: 2022/10/29
 * Time: 10:23 AM
 */

namespace app\common\cachemodel\zl2;

use app\common\model\BaseCache;
use XznsTools\cache\CacheManage;

class Zl2MentalTestExercise extends BaseCache
{
    protected $table = 'wechat_backup_07_18_14_43_00.zl2_mental_test_exercise';

    protected $cacheModel = 'astro_fortune.mental_test_exercise';

    public function getAllIds()
    {
        $resData = CacheManage::getInstance()
            ->setCacheConfig($this->cacheModel)
            ->setPage(-1)
            ->setSort('desc')
            ->getListPageDataIds(function ($isCache = true, $sortType = 'desc')
            {
                try
                {
                    $data = $this->where(['status' => 1])->order("id {$sortType}")->column('id');

                    $resData = [];
                    foreach ($data as $id)
                    {
                        $resData[$id] = ['id' => $id];
                    }

                    return $resData;
                }
                catch (\Exception $e)
                {
                    return [];
                }
            }, 'id', 0);

        return $resData['data'];
    }

    /**
     * 批量获取
     * @param $ids
     * @param $type
     * @param $sortType
     * @return array|false|string|void
     */
    public function getInfoByIdsInData($ids, $type, $sortType)
    {
        $res = [];
        try
        {
            $data = $this->where(['id' => ['in', $ids]])->column('extend,title,create_time', 'id');
            foreach ($data as $key => $datum)
            {
                $res[$key]['extend'] = $datum['extend'];
                $res[$key]['title'] = $datum['title'];
                $res[$key]['id'] = $datum['id'];
                $res[$key]['create_time'] = $datum['create_time'];
            }
        }
        catch (\Exception $e)
        {
            $res = [];
        }

        return $res;
    }

    public function batchGetInfoByIds($cids, $field = [])
    {
        $resData = parent::batchGetInfoByIds($cids, $field);

        return $this->dataInit($resData);
    }


    /**
     * 数据初始化
     * @param array $data
     * @return array
     */
    public function dataInit(array $data)
    {
        foreach ($data as $index => $itemData)
        {
            $data[$index] = arrayKeyData2int($itemData, ['id', 'create_time']);

            if (isset($itemData['extend']))
            {
                $data[$index]['extend'] = json_decode($itemData['extend'], true);
            }
        }

        return $data;
    }
}
